#!/bin/bash

ARCH=rk3588
src_dir=$HOME/kn/third-src/
install_dir=$(pwd)/third/$ARCH

run_dir=$(pwd)

[ -d $src_dir ] || mkdir -p $src_dir

if [ x"$ARCH" == x"rk3588" ]; then
    . $HOME/kn/tools/source-rk3588.sh
    CROSSCOMPILE=aarch64-none-linux-gnu-
fi

export CC=
export CXX=
export AR=
export RANLIB=

pkg_base=openssl-3.1.6
pkg_lib=$install_dir/$pkg_base/lib/libssl.a
if [ ! -f $pkg_lib ]; then
    echo "build openssl"
    pkg_pkg=$src_dir/openssl-3.1.6.tar.gz
    pkg_url="https://github.com/openssl/openssl/releases/download/openssl-3.1.6/openssl-3.1.6.tar.gz"
    if [ ! -f $pkg_pkg ]; then
        echo "download $pkg_pkg from $pkg_url"
        wget -O $pkg_pkg $pkg_url
    fi
    tar xvf $pkg_pkg -C $src_dir
    echo "Enter $src_dir/$pkg_base"
    cd $src_dir/$pkg_base
    if [ x"$CROSSCOMPILE" = x"" ]; then
        ./configure
    else
        ./Configure linux-aarch64 --cross-compile-prefix=$CROSSCOMPILE
    fi
    make clean
    make 
    make install DESTDIR=$install_dir/$pkg_base
    cd -
    echo "Exit ..."
    cd $install_dir/$pkg_base
    mv usr/local/* . 
    rm -rf usr
    cd -
fi

export CC=${CROSSCOMPILE}gcc
export CXX=${CROSSCOMPILE}g++
export AR=${CROSSCOMPILE}ar
export RANLIB=${CROSSCOMPILE}ranlib

sql_base=sqlite3
sql_lib=$install_dir/$sql_base/lib/libsqlite3.a
if [ ! -f $sql_lib ]; then
    echo "build sqlite3"
    sql_pkg=$src_dir/sqlite-autoconf-3460000.tar.gz
    sql_url="https://www.sqlite.org/2024/sqlite-autoconf-3460000.tar.gz"
    if [ ! -f $sql_pkg ]; then
        echo "download $sql_pkg from $sql_url"
        wget -O $sql_pkg $sql_url
    fi
    tar xvf $sql_pkg -C $src_dir
    echo "Enter $src_dir/sqlite-autoconf-3460000"
    cd $src_dir/sqlite-autoconf-3460000
    if [ x"$CROSSCOMPILE" = x"" ]; then
        ./configure
    else
        ./configure --host=arm-linux --target=arm-linux
    fi
    make clean
    make 
    make install DESTDIR=$install_dir/$sql_base
    cd -
    echo "Exit ..."
    cd $install_dir/$sql_base
    mv usr/local/* . 
    rm -rf usr
    cd -
fi

lib_base=jpeg-turbo
lib_base_lib=$install_dir/$sql_base/lib/libturbojpeg.so
if [ ! -f lib_base_lib ]; then
    echo "build jpeg-turbo"
    dirname=libjpeg-turbo-3.0.4
    sql_pkg=$src_dir/libjpeg-turbo-3.0.4.tar.gz
    sql_url="https://github.com/libjpeg-turbo/libjpeg-turbo/releases/download/3.0.4/libjpeg-turbo-3.0.4.tar.gz"
    if [ ! -f $sql_pkg ]; then
        echo "download $sql_pkg from $sql_url"
        wget -O $sql_pkg $sql_url
    fi
    tar xvf $sql_pkg -C $src_dir
    mkdir $src_dir/$dirname/build-${ARCH}
    echo "Enter $src_dir/$dirname/build-${ARCH}"
    cd $src_dir/$dirname/build-${ARCH}
    if [ x"$CROSSCOMPILE" = x"" ]; then
        cmake ..
    else
        cmake -DCMAKE_TOOLCHAIN_FILE=${run_dir}/tools/${ARCH}.cmake -DWITH_TURBOJPEG=OFF ..
    fi
    make clean
    make
    make install DESTDIR=$install_dir/$lib_base
    cd -
    echo "Exit ..."
    cd $install_dir/$lib_base
    mv usr/local/* .
    rm -rf usr
    cd -
fi

# lvgl
# use https://github.com/lvgl/lv_port_linux.git

echo "All done"
